package com.vj.analyzeFakeNames;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Map.Entry;

import junit.framework.Assert;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.junit.Test;

//SolrDocument doc = docs.get(i);
//System.out.println("\t"+doc.toString());
// it's good
public class Indexer {

	
	SolrServer server = new HttpSolrServer("http://192.168.71.145:8080/solr");
	
	@Test
	public void index_names() throws FileNotFoundException, IOException, SolrServerException{
		System.out.println(">>>>>>>>>>>>>>>> @ index_names");
		// delete all existing index first, to ensure cases within this class
		deleteAllExistingIndex();//TODO
//		Assert.assertTrue(false);
		// delete all done.		
		
		String fileName;
		int status = 0;
		
		fileName = "a.txt.transform.properties";
		status += indexSingalDoc(fileName);

		fileName = "b.txt.transform.properties";
		status += indexSingalDoc(fileName);

		fileName = "c.txt.transform.properties";
		status += indexSingalDoc(fileName);

		fileName = "d.txt.transform.properties";
		status += indexSingalDoc(fileName);
		
		fileName = "e.txt.transform.properties";
		status += indexSingalDoc(fileName);
		
		server.commit();
		
		Assert.assertEquals("Status is bad !!", 0, status);
	}

	// return INDEX STATUS
	private int indexSingalDoc (String fileName) throws FileNotFoundException, IOException, SolrServerException {
		String id = fileName;
		
		fileName = "src/com/vj/analyzeFakeNames/rawData/" + fileName;
		Properties p = new Properties();
		p.load(new FileReader(fileName));
		System.out.println(p.getProperty("name"));
		System.out.println(p.getProperty("address"));
		
		String name = p.getProperty("name");
		String address = p.getProperty("address");
		String Phone = p.getProperty("Phone");
		String Email_Address = p.getProperty("EmailAddress");
		String Username = p.getProperty("Username");
		String Password = p.getProperty("Password");
		String Birthday = p.getProperty("Birthday");
		String MasterCard = p.getProperty("MasterCard");
		String Expires = p.getProperty("Expires");
		
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", id);
		doc.addField("fn_name", name);
		doc.addField("fn_address", address);
		doc.addField("fn_phone", Phone);
		doc.addField("fn_email_address", Email_Address);
		doc.addField("fn_username", Username);
		doc.addField("fn_password", Password);
		doc.addField("fn_birthday", Birthday);
		doc.addField("fn_master_card", MasterCard);
		doc.addField("fn_expire_date", Expires);
		
		UpdateResponse ursp = server.add(doc);
		
		int status = ursp.getStatus(); // 0 : good.
		
		return status;
	}

	private void deleteAllExistingIndex() throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		UpdateResponse ursp = server.deleteByQuery("*:*");
		Assert.assertEquals("DELETE ALL FAILED!!", 0, ursp.getStatus());
	}
	
}
  